package com.coocaa.ops.admin.v1.auth.sys;


import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.annotation.Config;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.auth.sys.constant.MenuTokenConstants;
import com.coocaa.ops.admin.v1.auth.sys.model.query.SysMenuModel;
import com.coocaa.ops.admin.v1.auth.sys.service.impl.SysMenuServiceImpl;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.auth.entity.SysMenuEntity;
import com.coocaa.ops.common.tool.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2019/3/13.
 * @description
 */
@RestController
@Config(menuToken = MenuTokenConstants.SYS_MENU)
@RequestMapping("/v1/sysMenu")
public class SysMenuController extends MgrBaseController {
    @Autowired
    private SysMenuServiceImpl sysMenuService;
    private static CommonLanguageProperties commonLanguageProperties;

    /**
     * 普通树表查询分页
     *
     * @param model
     * @return
     * @throws Exception
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/treePageList", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String treePageList(SysMenuModel model) throws Exception {
        List<SysMenuEntity> menus = sysMenuService.queryAll(model);
        return JsonUtil.getListToTreeJson(menus, "menuId", "pmid", null);
    }

    /**
     * 获取菜单没有操作列表
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getNotRunByMenuId", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String getNotRunByMenuId(HttpServletRequest request, @RequestParam Integer menuId) {
        Map result = sysMenuService.getNotRunByMenuId(menuId);
        return render2Json(result);
    }

    /**
     * 获取菜单已有的操作列表
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getRunByMenuId", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String getRunByMenuId(HttpServletRequest request, @RequestParam Integer menuId) {
        Map result = sysMenuService.getRunByMenuId(menuId);
        return render2Json(result);
    }

    /**
     * 保存 菜单的操作权限信息
     *
     * @param request
     * @param menuId
     * @param runIds
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/saveMenuRun", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public Object saveMenuRun(HttpServletRequest request, @RequestParam Integer menuId, @RequestParam(value = "runIds", required = false) Integer[] runIds) {
        sysMenuService.saveMenuRuns(menuId, runIds);
        return R.ok();
    }

    /**
     * 保存
     *
     * @param request
     * @param jsonStr
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public Object save(HttpServletRequest request, String jsonStr) {
        SysMenuEntity entity = (SysMenuEntity) JsonUtil.json2Object(jsonStr, SysMenuEntity.class);
        sysMenuService.save(entity);
        return R.ok();
    }

    /**
     * 获取详情信息
     *
     * @param id
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getDetailInfo", method = {RequestMethod.GET})
    public Object getDetailInfo(Integer id) {
        InputParamsValidator.validIdValue(id);
        SysMenuEntity entity = sysMenuService.getDetailInfo(id);
        Map result = R.ok(entity);
        return render2Json(result);
    }


    /**
     * 分页数据
     *
     * @param request
     * @param model
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/pageList", method = {RequestMethod.GET, RequestMethod.POST})
    public Object pageList(HttpServletRequest request, SysMenuModel model) {
        Map<String, Object> data = sysMenuService.getPageResult(model);
        return render2Json(data);
    }

    /**
     * 更新排序
     *
     * @param request
     * @param id
     * @param seq
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.EDIT)
    @RequestMapping(value = "/updateSeq", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public Object updateSeq(HttpServletRequest request, Integer id, Integer seq) {
        sysMenuService.updateSeq(id, seq);
        return R.ok().msg(commonLanguageProperties.getUpdateSuccess());
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        SysMenuController.commonLanguageProperties = commonLanguageProperties;
    }

    /**
     * 删除记录
     *
     * @param
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.DELETE)
    @RequestMapping(value = "/remove", method = {RequestMethod.GET, RequestMethod.POST})
    public Object remove(@RequestParam Integer[] menuId, HttpServletRequest request) {
        return sysMenuService.deleteBatch(menuId, WebUtil.getCurrentUser(request));
    }

    /**
     * 获取菜单运行权限列表
     *
     * @param request
     * @param
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getMenuInfo", method = {RequestMethod.GET}, produces = "text/html;charset=UTF-8")
    public Object getMenuInfo(HttpServletRequest request) {
        Map result = getMenuInfo(this.getClass(), request);
        return render2Json(result);
    }
}
